package com.settlement.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.settlement.system.common.annotation.PreventDuplicateSubmit;
import com.settlement.system.common.result.PageResult;
import com.settlement.system.common.result.Result;
import com.settlement.system.model.entity.SysPlatform;
import com.settlement.system.model.form.PlatformForm;
import com.settlement.system.model.query.PlatformPageQuery;
import com.settlement.system.service.SysPlatformService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 平台设置控制器
 *
 */
@Tag(name = "04.平台设置接口")
@RestController
@RequestMapping("/api/v1/platform")
@RequiredArgsConstructor
@Slf4j
public class SysPlatformController {

    @Value("${system.config.secret}")
    private String secret;

    @Value("${system.config.appId}")
    private String appId;

    private final SysPlatformService platformService;
    @Operation(summary = "平台分页列表", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/page")
    public PageResult<SysPlatform> getStorePage(
            @ParameterObject PlatformPageQuery queryParams
    ) {
        Page<SysPlatform> result = platformService.getPlatformPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "保存店铺", security = {@SecurityRequirement(name = "Authorization")})
    @PostMapping
    @PreAuthorize("@ss.hasPerm('sys:platform:save')")
    @PreventDuplicateSubmit
    public Result<?> savePlatform(
            @RequestBody @Valid PlatformForm platformForm
    ) {
        boolean result = platformService.savePlatform(platformForm);
        return Result.judge(result);
    }

    @Operation(summary = "平台表单数据", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/{platformId}/form")
    public Result<SysPlatform> getPlatformForm(
            @Parameter(description = "平台ID") @PathVariable Long platformId
    ) {
        SysPlatform store = platformService.getPlatformFormData(platformId);
        return Result.success(store);
    }

    @Operation(summary = "所有平台列表", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/list")
    public Result<List<SysPlatform>> getPlatformList() {
        return Result.success(platformService.getPlatformList());
    }

    @Operation(summary = "删除平台", security = {@SecurityRequirement(name = "Authorization")})
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('sys:platform:delete')")
    public Result<?> deletePlatforms(
            @Parameter(description = "平台ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = platformService.deletePlatforms(ids);
        return Result.judge(result);
    }
}
